package net.j4love.spring.mvc.ex.bind;

import net.j4love.spring.mvc.ex.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author He Peng
 * @create 2017-06-11 21:30
 * @update 2017-06-11 21:30
 * @updatedesc : 更新说明
 * @see
 */

@Controller
@RequestMapping(
        path = "/bind"
)
public class DataBindController {

    @RequestMapping(
            path = "/request"
    )
    public ModelAndView requestBindHandler(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/request");
        modelAndView.addObject("request",request);
        return modelAndView;
    }

    @RequestMapping(
            path = "/response"
    )
    public ModelAndView responseBindHandler(HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/response");
        modelAndView.addObject("response",response);
        return modelAndView;
    }

    @RequestMapping(
            path = "/session"
    )
    public ModelAndView sessionBindHandler(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/session");
        modelAndView.addObject("session",session);
        return modelAndView;
    }

    @RequestMapping(
            path = "/cookie"
    )
    public ModelAndView cookieBindHandler(@CookieValue("JSESSIONID") String cookie) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/cookie");
        modelAndView.addObject("cookie",cookie);
        return modelAndView;
    }

    @RequestMapping(
            path = "/header"
    )
    public ModelAndView headerBindHandler(@RequestHeader("Accept-Language") String acceptLanguage) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/header");
        modelAndView.addObject("header",acceptLanguage);
        return modelAndView;
    }

    @RequestMapping(
            path = "/byte"
    )
    public ModelAndView byteBindHandler(Byte byt) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/byte");
        modelAndView.addObject("byt",byt);
        return modelAndView;
    }

    @RequestMapping(
            path = "/short"
    )
    public ModelAndView byteBindHandler(Short shot) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/short");
        modelAndView.addObject("shor",shot);
        return modelAndView;
    }

    @RequestMapping(
            path = "/bool"
    )
    public ModelAndView booleanBindHandler(boolean bool) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/bool");
        modelAndView.addObject("bool",bool);
        return modelAndView;
    }

    @RequestMapping(
            path = "/boolean"
    )
    public ModelAndView booleanBindHandler(Boolean bool) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/boolean");
        modelAndView.addObject("bool",bool);
        return modelAndView;
    }

    @RequestMapping(
            path = "/int"
    )
    public ModelAndView intBindHandler(int integer) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/integer");
        modelAndView.addObject("integer",integer);
        return modelAndView;
    }

    @RequestMapping(
            path = "/integer"
    )
    public ModelAndView integerBindHandler(Integer integer) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/integer");
        modelAndView.addObject("integer",integer);
        return modelAndView;
    }

    @RequestMapping(
            path = "/long"
    )
    public ModelAndView longBindHandler(Long lon) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/long");
        modelAndView.addObject("lon",lon);
        return modelAndView;
    }

    @RequestMapping(
            path = "/float"
    )
    public ModelAndView floatBindHandler(Float flot) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/float");
        modelAndView.addObject("float",flot);
        return modelAndView;
    }

    @RequestMapping(
            path = "/double"
    )
    public ModelAndView doubleBindHandler(Double doubl) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/double");
        modelAndView.addObject("double",doubl);
        return modelAndView;
    }

    @RequestMapping(
            path = "/string"
    )
    public ModelAndView stringBindHandler(String str) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/string");
        modelAndView.addObject("string",str);
        return modelAndView;
    }

    @RequestMapping(
            path = "/array"
    )
    public ModelAndView arrayBindHandler(String[] arr) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/array");
        modelAndView.addObject("array" , Arrays.toString(arr));
        return modelAndView;
    }

    @RequestMapping(
            path = "/list"
    )
    public ModelAndView listBindHandler(@RequestParam List<String> list) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/list");
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @RequestMapping(
            path = "/set"
    )
    public ModelAndView setBindHandler(@RequestParam Set<String> set) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/set");
        modelAndView.addObject("set",set);
        return modelAndView;
    }

    @RequestMapping(
            path = "/map"
    )
    public ModelAndView mapBindHandler(@RequestParam Map<Object,Object> map) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/map");
        modelAndView.addObject("map", map);
        return modelAndView;
    }

    @RequestMapping(
            path = "/simple/pojo"
    )
    public ModelAndView pojoBindHandler(User user) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/simple/pojo");
        modelAndView.addObject("pojo", user);
        return modelAndView;
    }

    @RequestMapping(
            path = "/simple/pojo/json"
    )
    public ModelAndView jsonBindHandler(@RequestBody User user) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/simple/pojo/json");
        modelAndView.addObject("pojo", user);
        return modelAndView;
    }

    @RequestMapping(
            path = "/same/name"
    )
    public ModelAndView sameNameArgBindHand(User user,String name) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/same/name");
        modelAndView.addObject("msg", user + " | " + name);
        return modelAndView;
    }

    @RequestMapping(
            path = "/list/pojo"
    )
    public ModelAndView listPojoBindHadnler(MultipleUserWrap userWrap) {
        ModelAndView modelAndView = new ModelAndView("parameterBind");
        modelAndView.addObject("path","/list/pojo");
        modelAndView.addObject("msg", userWrap);
        return modelAndView;
    }
}
